package com.chidopi.market.bookstore.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;

import com.chidopi.cdf.dao.AbstractBaseDaoImpl;
import com.chidopi.market.bookstore.domain.BookCities;
import com.chidopi.market.bookstore.domain.Subscribe4iOS;
import com.chidopi.market.bookstore.domain.Subscriptions;


public class Subscribe4iOSDaoImpl extends AbstractBaseDaoImpl<Subscribe4iOS>  {
	
	private static Logger logger = LoggerFactory.getLogger(Subscribe4iOSDaoImpl.class);

	@Override
	public int insert(Subscribe4iOS t) {
		String sql = "insert into subscribe4ios (SUBSCRIBE_ID, CITY_ID, APPGENRE, APPDEVICE, APPBOUNDLEID, APPSTOREPAGEL, APPSTOREPAGEP, APPSTOREICON, JOURNAL_ID, CREATE_DATE, CREATE_BY) "
				+ "values (:subscribe.id, :bookCity.id, :appGenre, :appDevice, :appBoundleID, :appStorePageL, :appStorePageP, :appStoreIcon, :journal_id,  NOW(), :create_by)";
		return super.execSQL(sql, t);
	}

	@Override
	public int update(Subscribe4iOS t) {
		String sql = "update subscribe4ios set SUBSCRIBE_ID = :subscribe.id, CITY_ID = :bookCity.id, APPGENRE = :appGenre, APPDEVICE = :appDevice, APPBOUNDLEID = :appBoundleID, APPSTOREPAGEL = :appStorePageL, APPSTOREPAGEP = :appStorePageP, APPSTOREICON = :appStoreIcon, JOURNAL_ID = :journal_id, update_by = :update_by where id= :id";
		return super.execSQL(sql, t);
	}

	@Override
	public int delete(Subscribe4iOS t) {
		String sql = "delete from subscribe4ios where id = :id";
		return super.execSQL(sql, t);
	}

	@Override
	public List<Subscribe4iOS> find(String sqlWhere, Object... args) {
		if (sqlWhere == null || sqlWhere.trim().equals("")) {
			String sql = "select a.*, b.NAME, c.CITYNAME from subscribe4ios a left join subscriptions b on a.SUBSCRIBE_ID = b.ID left join bookcities c on a.CITY_ID = c.ID order by a.id ";
			return super.simpleJdbcTemplate.query(sql, this.getRowMapper());
		} else {
			logger.debug("sqlWhere:[" + sqlWhere + "]/params:" + Arrays.asList(args).toString());
			String sql = "select a.*, b.NAME, c.CITYNAME from subscribe4ios a left join subscriptions b on a.SUBSCRIBE_ID = b.ID left join bookcities c on a.CITY_ID = c.ID where " + sqlWhere + " order by c.id";
			logger.debug("sql: " + sql);
			List<Subscribe4iOS> list = super.simpleJdbcTemplate.query(sql, this.getRowMapper(), args);
			if (list == null) {
				logger.debug("query null!!!");
			} else {
				for (Subscribe4iOS sub : list) {
					logger.debug("id=" + sub.getId() + "/Subscribe=" + sub.getSubscribe().getId());
				}
			}
			return list;
		}
	}

	@Override
	public Subscribe4iOS findById(Object id) {
		String sql = "a.id=?";
		List<Subscribe4iOS> list = this.find(sql, id);
		if (0 == list.size()) return null;
		return list.get(0);
	}

	@Override
	protected String getTable() {
		return "subscribe4ios";
	}

	@Override
	protected String getSequenceName() {
		return null;
	}

	@Override
	protected RowMapper<Subscribe4iOS> getRowMapper() {
		return new ParameterizedRowMapper<Subscribe4iOS>(){
			
			@Override
			public Subscribe4iOS mapRow(ResultSet rs, int rowNum)	throws SQLException {
				Subscribe4iOS vo = new Subscribe4iOS();
				vo.setId(rs.getInt("ID"));
				vo.setAppGenre(rs.getString("APPGENRE"));
				vo.setAppDevice(rs.getString("APPDEVICE"));
				vo.setAppBoundleID(rs.getString("APPBOUNDLEID"));
				vo.setAppStorePageL(rs.getString("APPSTOREPAGEL"));
				vo.setAppStorePageP(rs.getString("APPSTOREPAGEP"));
				vo.setAppStoreIcon(rs.getString("APPSTOREICON"));
				vo.setJournal_id(rs.getInt("JOURNAL_ID"));
				
				BookCities city = new BookCities();
				city.setId(rs.getInt("CITY_ID"));
				city.setCityname(rs.getString("CITYNAME"));
				vo.setBookCity(city);
				
				Subscriptions sub= new Subscriptions();
				sub.setId(rs.getInt("SUBSCRIBE_ID"));
				sub.setName(rs.getString("NAME"));
				vo.setSubscribe(sub);
				
				return vo;
			}
		};	
    }

}
